package vn.edu.saigontech.ncms.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.bookstore.ImportReceipt;
import vn.edu.saigontech.ncms.service.ImportReceiptService;
import vn.edu.saigontech.ncms.util.Checker;

@SuppressWarnings("unchecked")
public class ImportReceiptServiceImpl extends
		HibernateGenericService<ImportReceipt, Long> implements
		ImportReceiptService {

	@Override
	public Collection<ImportReceipt> findAll() {
		Criteria criteria = this.createCriteria();
		criteria.addOrder(Order.desc("date"));
		return criteria.list();
	}

	@Override
	public ImportReceipt findByNumber(String number) {
		Criteria criteria = this.createCriteria();
		criteria.add(Restrictions.eq("number", number));
		List<ImportReceipt> list = criteria.list();
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<ImportReceipt> findByDate(Date fromDate, Date toDate) {
		Criteria criteria = this.createCriteria();
		if (!Checker.isNull(fromDate)) {
			criteria.add(Restrictions.ge("date", fromDate));
		}
		if (!Checker.isNull(toDate)) {
			criteria.add(Restrictions.le("date", toDate));
		}
		criteria.addOrder(Order.asc("id"));
		return criteria.list();
	}
}